
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author user
 */
public class ModifClient extends javax.swing.JFrame {

    private String name;
    private String firstName;
    private String address;
    private String phone;
    private String mail;
    private String entreprise;
    private String job;
    private javax.swing.JScrollPane jScrollPane1;

    public ModifClient() {
        
        this.setTitle(" Ajouter Client ");
        //this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
        // *********Remplissage Jcombox de Trader **********************           
        DbManip.remplirComboBoxADeuxChamps(choixTrader, "trader", "nom_trader", "prenom_trader", "Choisir un trader");
        //**************************************************         
        DbManip.remplirComboBoxAUnChamps(choixEntreprise, "ENTREPRISE", "NOM_ENTREPRISE", "Choisir une entreprise");

        // jScrollPane1 = new javax.swing.JScrollPane(panscroll,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        // this.getContentPane().add(jScrollPane1);




        //**************************************************      
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        valider = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        panDonnee = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        choixEntreprise = new javax.swing.JComboBox();
        profession = new javax.swing.JTextField();
        choixTrader = new javax.swing.JComboBox();
        nom = new javax.swing.JTextField();
        prenom = new javax.swing.JTextField();
        adresse = new javax.swing.JTextField();
        telephone = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 500, 430));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        valider.setBackground(new java.awt.Color(255, 255, 255));
        valider.setText("Valider");
        valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validerActionPerformed(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setText("Effacer");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jSeparator1.setBackground(new java.awt.Color(255, 51, 0));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
        jSeparator1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.white, java.awt.Color.red, java.awt.Color.lightGray));

        jLabel4.setText("Téléphone");

        jLabel3.setText("Adresse");

        jLabel5.setText("Email");

        jLabel1.setText("Nom");

        jLabel6.setText("Profession");

        jLabel2.setText("Prénom");

        jLabel7.setText("Trader");

        email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                emailKeyTyped(evt);
            }
        });

        profession.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                professionKeyTyped(evt);
            }
        });

        nom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nomKeyTyped(evt);
            }
        });

        prenom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prenomActionPerformed(evt);
            }
        });
        prenom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                prenomKeyTyped(evt);
            }
        });

        adresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adresseActionPerformed(evt);
            }
        });
        adresse.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                adresseKeyTyped(evt);
            }
        });

        telephone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                telephoneActionPerformed(evt);
            }
        });
        telephone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                telephoneKeyTyped(evt);
            }
        });

        jLabel8.setText("Entreprise");

        javax.swing.GroupLayout panDonneeLayout = new javax.swing.GroupLayout(panDonnee);
        panDonnee.setLayout(panDonneeLayout);
        panDonneeLayout.setHorizontalGroup(
            panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDonneeLayout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(prenom)
                    .addComponent(adresse)
                    .addComponent(telephone)
                    .addComponent(email)
                    .addComponent(profession)
                    .addComponent(choixTrader, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(choixEntreprise, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        panDonneeLayout.setVerticalGroup(
            panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDonneeLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(12, 12, 12)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(adresse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(telephone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(profession, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(choixTrader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(21, 21, 21)
                .addGroup(panDonneeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(choixEntreprise, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(panDonnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 612, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(195, 195, 195)
                .addComponent(valider, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(130, 130, 130))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panDonnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(valider)
                    .addComponent(jButton2))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void prenomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prenomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_prenomActionPerformed

    private void adresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adresseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_adresseActionPerformed

    private void telephoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telephoneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_telephoneActionPerformed

    private void validerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validerActionPerformed
        try {
            name = nom.getText().toUpperCase();
            while (name.indexOf(" ") == 0) {
                name = name.substring(1, name.length());
            }
            if (!name.replace(" ", "").matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le nom", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }
            firstName = prenom.getText().toUpperCase();
            // skip spaces
            while (firstName.indexOf(" ") == 0) {
                firstName = firstName.substring(1, name.length());
            }

            if (!firstName.replace(" ", "").matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le prénom", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }
            address = adresse.getText();
            while (address.indexOf(" ") == 0) {
                address = address.substring(1, name.length());
            }

            if (!address.replace(" ", "").matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez l'adresse", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }

            mail = email.getText();
            if (!mail.replace(" ", "").matches(DbManip.regexmail)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le mail !", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }


            phone = telephone.getText().replace(" ", "");
            if (!phone.matches(DbManip.regexDigit)) {
                JOptionPane.showMessageDialog(null, "Vérifiez le numéro de téléphone", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }
            job = profession.getText();

            if (!job.replace(" ", "").matches(DbManip.regexLettreNumUnderscore)) {
                JOptionPane.showMessageDialog(null, "Vérifiez la profession", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }
            entreprise = choixEntreprise.getSelectedItem().toString();

            if (entreprise.equals("-- Choisir une entreprise--")) {
                JOptionPane.showMessageDialog(null, "Veuilez choisir une entreprise", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (choixTrader.getSelectedItem().toString().equals("-- Choisir un trader--")) {
                JOptionPane.showMessageDialog(null, "Veuilez choisir un trader", "Attention", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!(DbManip.existDsTable("Client", "Nom_Client", name, "PRENOM_CLIENT", firstName))) {

                if (JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?", "Confirmation ", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != 0) {
                    return;
                }

                //   Detreminer l' id_client Client à insérer automatiquement                
                int id_client = DbManip.next_Id("Client", "ID_CLIENT ");
                //**************************** Recherche id_entreprise à insérer automatiquement
                int id_entrep = 0;
                if (!"autre".equals(entreprise)) {
                    DbManip.requete = "SELECT ID_ENTREPRISE FROM ENTREPRISE WHERE NOM_ENTREPRISE='" + entreprise + "'";
                    DbManip.executer(DbManip.requete);

                    while (DbManip.rs.next()) {
                        id_entrep = DbManip.rs.getInt(1);
                    }
                }

                // ***** Recupérer matricule Trader ********************     
                String ch = (String) choixTrader.getSelectedItem();
                String ch1 = ch.substring(0, ch.indexOf(" "));
                String ch2 = ch.substring(ch.indexOf(" ") + 1);

                DbManip.requete = "SELECT MATRICULE FROM TRADER WHERE NOM_TRADER='" + ch2 + "' and PRENOM_TRADER ='" + ch1 + "'";
                DbManip.executer(DbManip.requete);
                int mat = 0;
                while (DbManip.rs.next()) {
                    mat = DbManip.rs.getInt(1);
                }
                //********************************************************************

                // *********************** Requete D'insertion dans la table Client***************************                      
                DbManip.requete = "insert into CLIENT VALUES ('" + id_client + "','" + mat + "','" + id_entrep + "','" + name + "','" + firstName + "','" + address + "','" + phone + "','" + mail + "','" + job + "')";
                DbManip.executerUpdate(DbManip.requete);
                //**********************************


                //************* Requete de creation d'un portefeuille associé au nouveau client ********
                // *** Detreminer l 'id_portefeuille à créer ********
                int id_pf = DbManip.next_Id("PORTE_FEUILLE", "ID_PORTE_FEUILLE ");
                // *********************** Requete D'insertion dans la table PORTE_FEUILLE***************************   

                DbManip.requete = "insert into PORTE_FEUILLE VALUES ('" + id_pf + "','" + id_client + "',TIMESTAMP '" + DbManip.date + "')";
                DbManip.executerUpdate(DbManip.requete);
                // *********************** Requete D'insertion dans la table Historique-PORTE_FEUILLE***************************
                DbManip.requete = "insert into HISTORIQUE_PORTEFEUILLE VALUES ('" + DbManip.next_Id("HISTORIQUE_PORTEFEUILLE", "ID_HPF") + "','" + id_pf + "','" + "0" + "',TIMESTAMP '" + DbManip.date + "')";
                DbManip.executerUpdate(DbManip.requete);

                JOptionPane.showMessageDialog(null, "Ajout du client avec succès !", "Information", JOptionPane.INFORMATION_MESSAGE);
                //**********************************

            }
            else {
                JOptionPane.showMessageDialog(null, "Ce nom d'utilisateur existe déja", "Erreur", JOptionPane.ERROR_MESSAGE);

            }

        }
        catch (SQLException ex) {
            Logger.getLogger(ModifClient.class.getName()).log(Level.SEVERE, null, ex);
        }




    }//GEN-LAST:event_validerActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        
        
        // TODO add your handling code here:
        nom.setText("");
        prenom.setText("");
        telephone.setText("");
        email.setText("");
        profession.setText("");
        adresse.setText("");
        choixTrader.setSelectedIndex(0);
        choixEntreprise.setSelectedIndex(0);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void nomKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomKeyTyped
        if (nom.getText().length() >= 12) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_nomKeyTyped

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped

        if (prenom.getText().length() >= 12) {
            evt.consume();// TODO add your handling code here:
    }//GEN-LAST:event_formKeyTyped
    }
    private void adresseKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_adresseKeyTyped
        if (adresse.getText().length() >= 25) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_adresseKeyTyped

    private void telephoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telephoneKeyTyped

        if (telephone.getText().length() >= 25) {
            evt.consume();// TODO add your handling code here:
    }//GEN-LAST:event_telephoneKeyTyped
    }
    private void emailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyTyped
        if (email.getText().length() >= 25) {
            evt.consume();        // TODO add your handling code here:
    }//GEN-LAST:event_emailKeyTyped
    }
    private void professionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_professionKeyTyped
        if (profession.getText().length() >= 25) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_professionKeyTyped

    private void prenomKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_prenomKeyTyped
        if (prenom.getText().length() >= 20) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_prenomKeyTyped
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField adresse;
    private javax.swing.JComboBox choixEntreprise;
    private javax.swing.JComboBox choixTrader;
    private javax.swing.JTextField email;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField nom;
    private javax.swing.JPanel panDonnee;
    private javax.swing.JTextField prenom;
    private javax.swing.JTextField profession;
    private javax.swing.JTextField telephone;
    private javax.swing.JButton valider;
    // End of variables declaration//GEN-END:variables
}
